#pragma once
#include "GLFWApplication.hpp"
#include <memory>
#include "FileSystem.h"
#include "File.h"
#include "Service.h"
#include "Task.h"
#include "OpenXLSX.hpp"
#include <map>

using namespace OpenXLSX;

struct SheetInfo
{
	std::map<unsigned, std::string> headInfoCol2Key;
	std::map<std::string, unsigned> headInfoKeyToCol;
	bool badSheet{ false };
	int maxCol{ 0 };
	bool GetColByHead(const std::string& head, int& col)
	{
		if (headInfoKeyToCol.count(head) > 0)
		{
			col = headInfoKeyToCol[head];
			return true;
		}
		return false;
	}
};

struct SheetBookInfo
{
	~SheetBookInfo() { if (book) delete book; book = nullptr; }
	std::string bookPath{};
	std::map<std::string, SheetInfo> sheets{};
	bool load{ false };
	XLDocument* book{ nullptr };
};

struct CompareBooks
{
	SheetBookInfo bookNt;
	SheetBookInfo bookOl;
};

class GameApp : public GLFWApplication
{
public:
	GameApp(GfxConfiguration &config)
		: GLFWApplication(config){};

	virtual int Initialize();
	virtual void Finalize();
	virtual void Tick();
	virtual void onSizeChange(int width, int height) override;

	void RenderUI();
	void InitStyle();

	void GenExcelInfo(const char* paht, SheetBookInfo& bookInfo);
	void GenHeadInfo(const XLWorksheet& sheet, SheetInfo& headInfo);

	void ShowDiff(CompareBooks* boos);
	void DrawHead();
	void MergeHead();

private:
	std::string pathTrunk_{};
	std::string pathOnline_{};
	std::shared_ptr<FileSystem> fileSystem_{};
	Service aysnServer_{};
	int pathChooseType_{ 0 };
	std::map<unsigned, CompareBooks*> compareInfo_{};
	bool showDiffs_{ false };
	bool showLoading_{ false };
	CompareBooks* curDispayCompare_{ nullptr };
	std::atomic<bool> error_{ false };

};
